#
# Copyright (c) 2011 CESNET
#
# LICENSE TERMS
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the Company nor the names of its contributors
#    may be used to endorse or promote products derived from this
#    software without specific prior written permission.
#
# ALTERNATIVELY, provided that this notice is retained in full, this
# product may be distributed under the terms of the GNU General Public
# License (GPL) version 2 or later, in which case the provisions
# of the GPL apply INSTEAD OF those given above.
#
# This software is provided ``as is'', and any express or implied
# warranties, including, but not limited to, the implied warranties of
# merchantability and fitness for a particular purpose are disclaimed.
# In no event shall the company or contributors be liable for any
# direct, indirect, incidental, special, exemplary, or consequential
# damages (including, but not limited to, procurement of substitute
# goods or services; loss of use, data, or profits; or business
# interruption) however caused and on any theory of liability, whether
# in contract, strict liability, or tort (including negligence or
# otherwise) arising in any way out of the use of this software, even
# if advised of the possibility of such damage.
#

NAME = @PACKAGE_NAME@

# Various configurable paths (remember to edit Makefile.in, not Makefile)
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
datadir = @datadir@
bindir = @bindir@
includedir = @includedir@
libdir =  @libdir@
mandir = @mandir@

CC = @CC@
LN_S = @LN_S@
DOXYGEN = @DOXYGEN@
DOXYGEN_CONFIG = Doxyfile
DOXYGEN_DIR = $(shell cat $(DOXYGEN_CONFIG) | grep '^OUTPUT_DIRECTORY' | sed 's/.*=//')
RPMBUILD = @RPMBUILD@
LDCONFIG = ldconfig
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
RPMDIR = RPMBUILD
LIBS = @LDFLAGS@ @LIBS@
CFLAGS = -Wall @CFLAGS@
CPPFLAGS = @CPPFLAGS@

OBJDIR= .obj
VERSION = $(shell cut -f1 ./VERSION | tr -d '\n')
MAJOR_VER = $(shell echo $(VERSION) | cut -d. -f1)
RELEASE = @RELEASE@

TOPDIR = @CONFIGURE_PWD@
SUBDIRS = @SUBDIRS@

SRCS =  src/callbacks.c \
	src/error.c \
	src/internal.c \
	src/messages.c \
	src/session.c \
	src/ssh.c \
	src/with_defaults.c \
	src/datastore.c \
	src/datastore/edit_config.c \
	src/datastore/empty/datastore_empty.c \
	src/datastore/file/datastore_file.c

HDRS_PUBL_ROOT = headers/libnetconf.h

HDRS_PUBL_SUBDIR = src/netconf.h \
	src/callbacks.h \
	src/error.h \
	src/session.h \
	src/messages.h \
	src/ssh.h \
	src/with_defaults.h \
	src/datastore.h
		
HDRS_PUBL = $(HDRS_PUBL_ROOT) $(HDRS_PUBL_SUBDIR)
SUBHEADERS_DIR = libnetconf

HDRS_PRIV = src/config.h.in \
	src/libnetconf.h \
	src/callbacks.h \
	src/error.h \
	src/messages.h \
	src/messages_internal.h \
	src/netconf.h \
	src/netconf_internal.h \
	src/session.h \
	src/ssh.h \
	src/with_defaults.h \
	src/datastore.h \
	src/datastore/datastore_internal.h \
	src/datastore/edit_config.h \
	src/datastore/empty/datastore_empty.h \
	src/datastore/file/datastore_file.h

OBJS = $(SRCS:%.c=$(OBJDIR)/%.o)

all: $(NAME).a $(NAME).so $(SUBDIRS)

.PHONY: $(SUBDIRS)

$(SUBDIRS):
	$(MAKE) -C $@

.PHONY: doc doxygen
doc: doxygen

$(NAME).a: $(OBJS)
	@rm -f $@
	ar rcs $@ $(OBJS)

$(NAME).so: $(OBJS)
	@rm -f $@;
	$(CC) -shared $(OBJS) $(LIBS) -Wl,-soname -Wl,$@.$(MAJOR_VER) -o $@.$(VERSION);
	@[ ! -L $@.$(MAJOR_VER) ] || rm -rf $@.$(MAJOR_VER);
	$(LN_S) $@.$(VERSION) $@.$(MAJOR_VER);
	@[ ! -L $@ ] || rm -rf $@;
	$(LN_S) $@.$(VERSION) $@;

$(OBJDIR)/%.o: %.c
	@[ -d $$(dirname $@) ] || \
		(mkdir -p $$(dirname $@))
	$(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDES) $(DBG) -fPIC -c $< -o $@

doxygen: $(DOXYGEN_CONFIG)
	@if [ -n "$(DOXYGEN)" ]; then \
		[ -d $(DOXYGEN_DIR) ] || mkdir -p $(DOXYGEN_DIR); \
		$(DOXYGEN); \
	else \
		echo "Missing doxygen"; \
		exit 1; \
	fi

.PHONY: dist
dist: $(NAME).spec tarball rpm

.PHONY: tarball
tarball: $(SRCS) $(HEADERS)
	@rm -rf $(NAME)-$(VERSION);
	@mkdir $(NAME)-$(VERSION);
	@for i in $(SUBDIRS) ; do $(MAKE) -C $$i tarball-prepare; done; \
	for i in $(SRCS) $(HDRS_PUBL) $(HDRS_PRIV) configure.in configure \
	    Makefile.in VERSION $(NAME).spec.in $(NAME).pc.in install-sh \
	    config.sub config.guess Doxyfile.in doc/img/cesnet-logo-125.png; do \
	    [ -d $(NAME)-$(VERSION)/$$(dirname $$i) ] || (mkdir -p $(NAME)-$(VERSION)/$$(dirname $$i)); \
		cp $$i $(NAME)-$(VERSION)/$$i; \
	done;
	@rm -rf $(RPMDIR)/SOURCES/; \
	mkdir -p $(RPMDIR)/SOURCES/; \
	tar -c -z -f $(RPMDIR)/SOURCES/$(NAME)-$(VERSION)-$(RELEASE).tar.gz \
	    $(NAME)-$(VERSION); \
	rm -rf $(NAME)-$(VERSION);

.PHONY: rpm
rpm: tarball
	mkdir -p $(RPMDIR)/BUILD $(RPMDIR)/RPMS $(RPMDIR)/SRPMS
	$(RPMBUILD) -ba $(NAME).spec \
		--define "_topdir `pwd`/$(RPMDIR)";

.PHONY: cleanall
cleanall: clean distclean

.PHONY: distclean
distclean:
	rm -rf $(RPMDIR)

.PHONY: install
install: all install-devel install-doc
	[ -d $(DESTDIR)/$(libdir) ] || \
	    (mkdir -p $(DESTDIR)/$(libdir); chmod 755 $(DESTDIR)/$(libdir))
	$(INSTALL_PROGRAM) $(NAME).so.$(VERSION) $(DESTDIR)/$(libdir)/;
	@[ ! -L $(DESTDIR)/$(libdir)/$(NAME).so.$(MAJOR_VER) ] || \
	    rm -rf $(DESTDIR)/$(libdir)/$(NAME).so.$(MAJOR_VER);
	$(LN_S) $(NAME).so.$(VERSION) $(DESTDIR)/$(libdir)/$(NAME).so.$(MAJOR_VER);

.PHONY: install-devel
install-devel:
	@[ -d $(DESTDIR)/$(libdir) ] || \
	    (mkdir -p $(DESTDIR)/$(libdir); chmod 755 $(DESTDIR)/$(libdir)); \
	[ -d $(DESTDIR)/$(libdir)/pkgconfig ] || \
	    (mkdir -p $(DESTDIR)/$(libdir)/pkgconfig; chmod 755 $(DESTDIR)/$(libdir)/pkgconfig); \
	[ -d $(DESTDIR)/$(includedir) ] || \
	    (mkdir -p $(DESTDIR)/$(includedir); chmod 755 $(DESTDIR)/$(includedir)); \
	[ -d $(DESTDIR)/$(includedir)/$(SUBHEADERS_DIR) ] || \
		(mkdir -p $(DESTDIR)/$(includedir)/$(SUBHEADERS_DIR));
	$(INSTALL_PROGRAM) $(NAME).a $(DESTDIR)/$(libdir)/;
	@[ ! -L $(DESTDIR)/$(libdir)/$(NAME).so ] || \
		rm -rf $(DESTDIR)/$(libdir)/$(NAME).so;	
	$(LN_S) $(NAME).so.$(VERSION) $(DESTDIR)/$(libdir)/$(NAME).so; \
	for i in $(HDRS_PUBL_ROOT); do \
		$(INSTALL_DATA) $$i $(DESTDIR)/$(includedir)/$$(basename $$i); \
	done; \
	for i in $(HDRS_PUBL_SUBDIR); do \
		$(INSTALL_DATA) $$i $(DESTDIR)/$(includedir)/$(SUBHEADERS_DIR)/$$(basename $$i); \
	done; \
	$(INSTALL_DATA) $(NAME).pc $(DESTDIR)/$(libdir)/pkgconfig/$(NAME).pc; \
	for i in $(SUBDIRS) ; do \
		$(MAKE) DESTDIR=$(patsubst .%,$(TOPDIR)/.%,$(DESTDIR)) -C $$i install \
		|| exit 1; \
	done

.PHONY: install-doc
install-doc: doc
	[ -d $(DESTDIR)/$(datadir)/$(NAME)/doxygen ] || \
		(mkdir -p $(DESTDIR)/$(datadir)/$(NAME)/doxygen; \
		chmod -R 755 $(DESTDIR)/$(datadir)/$(NAME))
	cp -r $(DOXYGEN_DIR)/* $(DESTDIR)/$(datadir)/$(NAME)/doxygen/;

.PHONY: clean
clean:
	for i in $(SUBDIRS) ; do $(MAKE) -C $$i clean ; done
	rm -rf *.a *.so* $(OBJDIR) $(DOXYGEN_DIR); 
